package com.etc.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.etc.entity.Restaurant;
import com.etc.service.RestaurantService;
import com.etc.utils.CommonMessage;

/**
 * 后台餐厅管理控制器
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping(value = "adminapi")
public class ARestaurantController {
	@Resource
	private RestaurantService restaurantService;

	@RequestMapping(value = "restaurantlist")
	public String toRestaurant(Model model) {
		return "admin/picture-list";
	}

	/**
	 * 管理员界面查询显示所有餐厅
	 * 
	 * @param page
	 * @param pagesize
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "showRestaurant")
	public String selectAllrestaurant( Model model) {
		
		List<Restaurant> list = restaurantService.getRestaurants();
		model.addAttribute("list", list);
		return "admin/picture-list";

	}

	/**
	 * 商家界面查询显示所有餐厅
	 * 
	 * @return
	 */
	@RequestMapping(value = "showRestaurantbyuid/{uid}")
	public String selectAllrestaurant(@PathVariable("uid") Integer uid , Model model) {
		List<Restaurant> list = restaurantService.getRestaurant(uid);
		model.addAttribute("list", list);
		return "admin/picture-list";

	}

	@RequestMapping(value = "toaddrestaurant")
	public String toaddrestaurant() {
		return "admin/picture-add";
	}
	/**
	 * 新增餐厅
	 * @param uid
	 * @param rname
	 * @param raddress
	 * @param rcontent
	 * @param rtel
	 * @param rprovince
	 * @param rcity
	 * @param file
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "addrestaurant")
	public String addRestaurant(Integer uid, String rname, String raddress, String rcontent, String rtel,
			String rprovince, String rcity, @RequestParam(value = "myfile") MultipartFile file,
			HttpServletRequest request) {
		String str = file.getOriginalFilename();
		String[] arr = str.split("\\.");
		if (arr[arr.length - 1].equals("jpg")||arr[arr.length - 1].equals("png")) {
			// 获取上传路径
			String path = "D:\\imgs\\restaurant" + "\\" + file.getOriginalFilename();
			// 创建目标文件
			File destFile = new File(path);
			try {
				FileUtils.copyInputStreamToFile(file.getInputStream(), destFile);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String rimg="/imgUrl/restaurant/"+file.getOriginalFilename();
			Restaurant restaurant= new Restaurant(null, rname, raddress, rcontent, rimg, rtel, rprovince, rcity, uid);
			boolean flag=restaurantService.addRestaurant(restaurant);
			if(flag) {
				return "redirect:showRestaurantbyuid/"+uid;
			}else {
				request.setAttribute("msg", "新增失败");
			}
			
			request.setAttribute("msg", "新增成功");
			return "redirect:showRestaurantbyuid/"+uid;
		} else {
			request.setAttribute("msg", "新增失败");
			return "redirect:showRestaurantbyuid/"+uid;
		}
	}
	@RequestMapping(value="delrestaurant/{rid}",method=RequestMethod.DELETE)
	@ResponseBody
	public CommonMessage delrestaurant(@PathVariable(value="rid") int rid) {
		CommonMessage cm = new CommonMessage();
		boolean flag = restaurantService.delrestaurant(rid);
		cm.setMsg(flag?"删除成功":"删除失败");
		return cm;
	}
	@RequestMapping(value="updaterestaurant")
	@ResponseBody
	public CommonMessage editrestaurant(@RequestBody Restaurant restaurant) {
		CommonMessage cm = new CommonMessage();
		boolean flag = restaurantService.updaterestaurant(restaurant);
		cm.setMsg(flag?"修改成功":"修改失败");
		return cm;
	}
	@RequestMapping(value="toupdaterestaurant")
	public String toupdaterestaurant(Restaurant restaurant, Model model) {
		model.addAttribute("restaurant", restaurant);
		return "admin/restaurant-update";
	}
	
}
